package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cn.hnu.qq.client.controller.LogonController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.global.MessageType;
import javax.swing.ImageIcon;

public class RegisterDialog extends JDialog implements ActionListener,MessageListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel(){
		@Override
		protected void paintComponent(Graphics g) {
			Image image = null;
			try {
				image = ImageIO.read(new File("src/main/java/image/02.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			};
					
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

		}
	};
	private JTextField textField;
	private JPasswordField passwordField;
	private JTextField textField_1;
	private JPasswordField passwordField_1;
	private JTextField textField_2;

	private User user;
	
	private LogonController controller = LogonController.getInstance();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			RegisterDialog dialog = new RegisterDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public RegisterDialog() {
		init();
	}

	public RegisterDialog(Dialog owner, boolean modal) {
		super(owner, modal);
		init();
	}

	private void init() {
		try {
			setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		setBounds(100, 100, 425, 240);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblAccount = new JLabel("account：");
		lblAccount.setBounds(47, 13, 66, 15);
		contentPanel.add(lblAccount);
		
		textField = new JTextField();
		textField.setBounds(135, 13, 134, 18);
		contentPanel.add(textField);
		textField.setColumns(10);
		
		JLabel lblPassword = new JLabel("password：");
		lblPassword.setBounds(47, 60, 87, 15);
		contentPanel.add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(135, 60, 134, 18);
		contentPanel.add(passwordField);
		
		JLabel lblEmail = new JLabel("email：");
		lblEmail.setBounds(47, 116, 54, 15);
		contentPanel.add(lblEmail);
		
		textField_1 = new JTextField();
		textField_1.setBounds(135, 116, 134, 18);
		contentPanel.add(textField_1);
		textField_1.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setBounds(279, 29, 118, 89);
		contentPanel.add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(RegisterDialog.class.getResource("/image/xiaohui.jpg")));
		panel.add(lblNewLabel, BorderLayout.CENTER);
		
		JLabel lblRepassword = new JLabel("RePassword：");
		lblRepassword.setBounds(36, 88, 98, 15);
		contentPanel.add(lblRepassword);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(135, 88, 134, 18);
		contentPanel.add(passwordField_1);
		
		JLabel lblNickname = new JLabel("nickName：");
		lblNickname.setBounds(41, 35, 72, 15);
		contentPanel.add(lblNickname);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(135, 35, 134, 18);
		contentPanel.add(textField_2);
		
		ButtonGroup bg = new ButtonGroup();
		
		JRadioButton radioButton = new JRadioButton("男");
		radioButton.setBounds(41, 151, 121, 23);
		contentPanel.add(radioButton);
		radioButton.addActionListener(this);
		
		bg.add(radioButton);
		
		radioButton.setSelected(true);
		radioButton.setActionCommand("boy");
		JRadioButton radioButton_1 = new JRadioButton("女");
		radioButton_1.setBounds(164, 151, 121, 23);
		radioButton_1.addActionListener(this);
		radioButton_1.setActionCommand("girl");
		bg.add(radioButton_1);
		
		contentPanel.add(radioButton_1);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
				okButton.addActionListener(this);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
				cancelButton.addActionListener(this);
			}
		}
	}

	public RegisterDialog(Frame owner, boolean modal) {
		super(owner, modal);
		init();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("OK")){
			if(textField.getText() == null || textField.getText().equals("")){
				JOptionPane.showMessageDialog(this, "账号不能为空");
				return;
			}
			if(textField_2.getText() == null || textField_2.getText().equals("")){
				JOptionPane.showMessageDialog(this, "昵称不能为空");
				return;
			}
			if(! new String(passwordField.getPassword()).equals(new String(passwordField_1.getPassword()))){
				JOptionPane.showMessageDialog(this, "两次输入密码不一致！");
				return;
			}
			if(passwordField.getPassword().length <= 0  || passwordField_1.getPassword().length <= 0){
				JOptionPane.showMessageDialog(this, "密码不能为空！");
				return;
			}
			String rex = "^[a-zA-Z1-9_]+@[a-zA-Z1-9_]+\\.[a-zA-Z1-9_]{1,4}$";
			if(! textField_1.getText().matches(rex)){
				JOptionPane.showMessageDialog(this, "邮箱填写不正确！");
				return;
			}
			user = new User();
			user.setAcount(textField.getText());
			user.setPassword(new String(passwordField.getPassword()));
			user.setEmail(textField_1.getText());
			user.setNickName(textField_2.getText());
			controller.regist(user);
		}
		if(e.getActionCommand().equals("Cancel")){
			this.dispose();
		}
		if(e.getActionCommand().equals("boy")){
			JRadioButton jrb = (JRadioButton) e.getSource();
			user.setSex(jrb.isSelected());
		}
		
	}

	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		
		if(message.getMessageType() == MessageType.ACCOUNT_EXIST){
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					JOptionPane.showMessageDialog(RegisterDialog.this, "账号已经存在");
				}
			});
		}
		if(message.getMessageType() == MessageType.REGIST_SUCCESS){
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					JOptionPane.showMessageDialog(RegisterDialog.this, "账号注册成功");
					RegisterDialog.this.dispose();
				}
			});
		}
		
	}

}
